<template>
  <div class="app-container">

    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="用户昵称: ">
        <el-input v-model="userQuery.nickname" placeholder="输入用户昵称"/>
      </el-form-item>
      <el-form-item label="手机号码: ">
        <el-input v-model="userQuery.mobile" placeholder="输入手机号码"/>
      </el-form-item>
      <el-form-item label="用户类型: ">
        <el-select v-model="userQuery.sign" clearable placeholder="选择用户类型">
          <el-option :value="0" label="普通用户"/>
          <el-option :value="1" label="教师用户"/>
        </el-select>
      </el-form-item>

      <el-button type="primary" icon="el-icon-search" @click="getList()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!-- 表格 -->
    <el-table
      :data="userList"
      border
      fit
      :header-cell-style="{background:'#0078AD',color:'#FFFFFF'}"
      highlight-current-row>

      <el-table-column
        label="序号"
        width="70"
        align="center">
        <!-- slot-scope="scope" 属性本质就是一个插槽 -->
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>

      <el-table-column prop="id" label="唯一标识" />
      <el-table-column prop="nickname" label="名称" />
      <el-table-column prop="mobile" label="手机号码" />
      <el-table-column
        label="头像"
        align="center">
        <template slot-scope="scope">
            <!-- <el-avatar size="80" :src="scope.row.cover"></el-avatar> -->
            <img :src="scope.row.avatar" style="width: 80px; height: 80px;"/>
        </template>
      </el-table-column>
      <el-table-column label="用户类型" width="80">
        <template slot-scope="scope">
          {{ scope.row.sign==='1'?'教师用户':'普通用户' }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <router-link :to="'/user/editUser/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />


  </div>
</template>

<script>
import user from '@/api/edu/user'

export default {
    data() {
        return {
          userList:null,//查询之后接口返回集合
          page:1,//当前页
          limit:5,//每页记录数
          total:0,//总记录数
          userQuery:{} //条件封装对象
        }
    },
    created() {
        this.getList()
    },
    methods: {
        getList(page = 1) {
            this.page = page
            user.pageUserCondition(this.page, this.limit, this.userQuery)
                .then(response => {
                    this.userList = response.data.rows
                    this.total = response.data.total
                })
                .catch(error => {
                    console.log(error)
                })
        },
        //清空条件查询内容
        resetData() {
            //清空内容
            this.userQuery = {}
            //重新查询列表
            this.getList()
        },
        //删除
        removeDataById(id) {
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            //发起请求
            user.deleteUserById(id)
                .then(response => {
                    this.getList((this.total % this.limit == 1 && this.page > 1 ? this.page - 1 : this.page))
                    this.$message({
                    type: 'success',
                    message: '删除成功!'
                  });
                })
          }).catch((response) => { // 失败
            if (response === 'cancel') {
              this.$message({
                type: 'info',
                message: '已取消删除'
              })
            }
          });
        },
    },
}

</script>
